<?php 

	//echo '<pre >';
	//var_dump($_FILES);

	require "Mysql.php";
	
	class imgUpload extends Mysql{
		
		private $filetype;
		private $fileinfo;
		private $path = './upload/';
		
		private $info;
		private $newName;
		
		public function __construct() {
			
			parent::__construct();
			$this->fileinfo = $_FILES;
			$this->filetype = array('image/jpeg', 'image/jpg', 'image/ico', 'image/gif', 'image/png');
			
		}
		
		public function check() {
			if($this->fileError($this->fileinfo["error"])){
				exit();
			}
			
			if($this->fileType($this->fileinfo["type"])){
				exit();
			}
			
			return 1;
		}
		
		public function upload(){
			if(!is_uploaded_file($this->fileinfo["name"])){
				move_uploaded_file($this->fileinfo["name"], $this->path . $this->newName());
				echo '文件上传成功！.';
				return 1;
			}else{
				echo '非法上传.';
				return 0;
			}
		}
		
		private function fileError($error){
			if($error > 0){
				switch($error){
					case 1:
						echo '文件大小超过限定的值.';
						return 1;
					case 2:
						echo '文件大小超过限定的值.';
						return 1;
					case 3:
						echo '文件只有部分上传.';
						return 1;
					case 4:
						echo '没有文件上传.';
						return 1;
					case 5:
						echo '文件上传为失败.';
						return 1;
					default:
						echo '文件上传失败.';
						return 1;
				}
			}
			return 0;
		}
		
		private function fileType($type){
			if(!in_array($type, $this->filetype)){
				echo '文件类型不符.';
				return 0;
			}
			return 1;
		}
		
		private function newName(){
			$this->info = pathinfo($fileinfo["name"]);
			do{
				$this->newName = date('YmdHis') . rand(1000,2000) . $this->info["exitention"];
			}
			while(file_exists($this->path));
			return $this->newName;
		}
		
	}
	
	$img = new imgUpload();
	
	$img->check();
	
	$img->upload();
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
